﻿#region Usings

using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using System.Xml;
using McTools.Xrm.Connection;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Metadata;
using RibbonEditor._Global;
using RibbonEditor.Actions;
using RibbonEditor.Actions.Parameters;
using RibbonEditor.AppCode;
using RibbonEditor.CommandUIDefinitionClasses;
using RibbonEditor.Controls;
using RibbonEditor.Controls.General;
using RibbonEditor.Controls.LocLabels;
using RibbonEditor.Controls.Rules;
using RibbonEditor.Forms;
using RibbonEditor.Forms.Entities;
using RibbonEditor.Forms.Solutions;
using RibbonEditor.LocLabels;
using RibbonEditor.Managers;
using RibbonEditor.Rules;
using RibbonEditor.Templates;
using Tanguy.WinForm.Utilities.DelegatesHelpers;

#endregion

namespace RibbonEditor
{
    public partial class MainForm : Form
    {
        #region Variables

        /// <summary>
        /// Crm Organization Service
        /// </summary>
        internal static IOrganizationService service;

        /// <summary>
        /// Solution Manager
        /// </summary>
        internal static SolutionManager sManager;

        internal static XmlNode currentRibbon;

        /// <summary>
        /// Ribbon Definition object
        /// </summary>
        RibbonDiffXml ribbonDiffXml;

        MetadataManager mManager;

        /// <summary>
        /// Current displayed control
        /// </summary>
        UserControl currentDisplayedControl;

        CrmConnectionStatusBar ccsb;
        ConnectionManager cManager;

        #endregion

        #region Constructors

        public MainForm()
        {
            InitializeComponent();

            this.Text += " (v" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString() + ")";

            this.cManager = new ConnectionManager(this);
            this.cManager.ConnectionSucceed += new ConnectionManager.ConnectionSucceedEventHandler(cManager_ConnectionSucceed);
            this.cManager.ConnectionFailed += new ConnectionManager.ConnectionFailedEventHandler(cManager_ConnectionFailed);
            this.cManager.StepChanged += new ConnectionManager.StepChangedEventHandler(cManager_StepChanged);

            ccsb = new CrmConnectionStatusBar(this.cManager);
            this.Controls.Add(ccsb);

            this.ManageActionsDisplay();
        }

        void cManager_StepChanged(object sender, StepChangedEventArgs e)
        {
            this.ccsb.SetMessage(e.CurrentStep);
        }

        void cManager_ConnectionFailed(object sender, ConnectionFailedEventArgs e)
        {
            this.ccsb.SetMessage("Error: " + e.FailureReason);
            MessageBox.Show("Error: " + e.FailureReason, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        void cManager_ConnectionSucceed(object sender, ConnectionSucceedEventArgs e)
        {
            service = e.OrganizationService;

            this.ccsb.SetConnectionStatus(true, e.ConnectionDetail);
            this.ccsb.SetMessage(string.Empty);

            if (e.Parameter != null)
            {
                if (e.Parameter.ToString() == "OpenSolution")
                {
                    this.ccsb.SetMessage("Caching entities metadata...");

                    this.mManager = new MetadataManager(service);

                    this.ccsb.SetMessage(string.Empty);

                    sManager = new SolutionManager(service);
                    if (sManager.OpenSolution(this, null, this.mManager, this.ccsb))
                    {
                        this.LoadRibbonsList();
                        this.ManageActionsDisplay();
                    }
                }
            }
        }

        #endregion

        #region General methods

        private void LoadRibbonTreeView(XmlNode ribbonXmlNode)
        {
            try
            {
                this.treeRibbon.Nodes.Clear();

                ribbonDiffXml = new RibbonDiffXml(ribbonXmlNode);

                TreeNode ribbonNode = new TreeNode("RibbonDiffXml")
                {
                    Tag = ribbonDiffXml
                };

                this.treeRibbon.Nodes.Add(ribbonNode);

                ribbonDiffXml.BuildTreeViewElement(this.treeRibbon.TopNode);

                this.treeRibbon.TopNode.Expand();
            }
            catch (Exception error)
            {
                CommonDelegates.DisplayMessageBox(this, "Error while loading Ribbon: " + error.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void ManageActionsDisplay()
        {
            MethodInvoker mi = delegate
            {
                this.tsbMainSave.Enabled = ListViewDelegates.GetSelectedItems(this.lvItems).Count > 0;
                this.tsbMainSaveFiles.Enabled = ListViewDelegates.GetSelectedItems(this.lvItems).Count > 0;
                this.tsbDelete.Enabled = ListViewDelegates.GetSelectedItems(this.lvItems).Count > 0;
                this.tssbAdd.Enabled = sManager != null && sManager.CustomizationFile != null;
                this.addEntityRibbonToolStripMenuItem.Enabled = sManager != null && sManager.CustomizationFile != null;
                this.addApplicationRibbonToolStripMenuItem.Enabled = ListViewDelegates.GetItems(this.lvItems).Find(delegate(ListViewItem item) { return item.Text == "Application Ribbon"; }) == null && sManager != null && sManager.CustomizationFile != null;
                this.addXmlContentToolStripMenuItem.Enabled = sManager != null && sManager.CustomizationFile != null;

                this.tsbRibbonDelete.Enabled = this.treeRibbon.SelectedNode != null && UIActionHelper.CanBeDeleted(this.treeRibbon.SelectedNode.Text);

                this.tsbItemDisplayXml.Enabled = this.treeRibbon.SelectedNode != null;
                this.tsbItemSave.Enabled = this.currentDisplayedControl != null;
            };

            if (this.toolStripMain.InvokeRequired)
            {
                this.toolStripMain.Invoke(mi);
            }
            else
            {
                mi();
            }
        }

        #endregion

        #region Layout methods

        private void DisplayInformationControl(UserControl ctrl)
        {
            if (ctrl != null)
            {
                if (this.panelContainer.Controls.Find("informationControl", true).Length > 0)
                {
                    this.panelContainer.Controls.Remove(this.Controls.Find("informationControl", true)[0]);
                }

                ctrl.Name = "informationControl";
                ctrl.Width = this.panelContainer.Width;
                ctrl.Height = this.panelContainer.Height;
                ctrl.Location = new Point(0, 0);//GetControlLocation();
                ctrl.Anchor = AnchorStyles.Bottom | AnchorStyles.Top | AnchorStyles.Right;

                this.panelContainer.Controls.Add(ctrl);

                this.currentDisplayedControl = ctrl;
            }
            else
            {
                if (this.panelContainer.Controls.Find("informationControl", true).Length > 0)
                {
                    this.panelContainer.Controls.Remove(this.panelContainer.Controls.Find("informationControl", true)[0]);
                    this.currentDisplayedControl = null;
                }
            }
        }

        private Point GetControlLocation()
        {
            int x = this.treeRibbon.Location.X + this.treeRibbon.Width + 10;
            int y = this.toolStripItem.Location.Y + this.toolStripItem.Height + 10;

            return new Point(x, y);
        }

        internal void RefreshLocalizedLabels()
        {
            this.treeRibbon.Nodes[0].Nodes[4].Nodes.Clear();

            foreach (XmlNode xmlNode in sManager.CustomizationFile.SelectNodes("ImportExportXml/RibbonDiffXml/LocLabels/LocLabel"))
            {
                TreeNode newNode = new TreeNode("LocLabel");

                LocLabel lb = new LocLabel(xmlNode);
                newNode.Tag = lb;

                this.treeRibbon.Nodes[0].Nodes[4].Nodes.Add(newNode);
            }
        }

        internal void RefreshRules()
        {
            //throw new NotImplementedException();
        }

        #endregion

        #region ToolStripMenu Buttons Handlers

        private void tsbMainOpenSolution_Click(object sender, EventArgs e)
        {
            if (sManager == null && this.mManager == null)
            {
                this.cManager.AskForConnection("OpenSolution");
            }
            else
            {
                if (sManager.OpenSolution(this, null, this.mManager, this.ccsb))
                {
                    this.LoadRibbonsList();
                    this.ManageActionsDisplay();
                }
            }
        }

        private void LoadRibbonsList()
        {
            ListViewDelegates.ClearItems(this.lvItems);

            foreach (XmlNode node in sManager.CustomizationFile.SelectNodes("//RibbonDiffXml"))
            {
                ListViewItem item = null;

                if (node.ParentNode.Name == "Entity")
                {
                    item = new ListViewItem(node.ParentNode.SelectSingleNode("Name").Attributes["LocalizedName"].Value);
                    item.Group = this.lvItems.Groups["lvgEntities"];
                    item.Tag = node;
                }
                else
                {
                    item = new ListViewItem("Application Ribbon");
                    item.Group = this.lvItems.Groups["lvgGeneral"];
                    item.Tag = node;
                }

                ListViewDelegates.AddItem(this.lvItems, item);
            }
        }

        private void tsbMainSave_Click(object sender, EventArgs e)
        {
            MethodInvoker process = delegate
            {
                this.Cursor = Cursors.WaitCursor;

                try
                {
                    if (this.lvItems.SelectedItems.Count > 0)
                        this.lvItems.SelectedItems[0].Tag = ((RibbonDiffXml)this.treeRibbon.TopNode.Tag).GetRibbonDiffXmlDocument().SelectSingleNode("RibbonDiffXml");

                    foreach (ListViewItem item in this.lvItems.Items)
                    {
                        if (item.Text == "Application Ribbon")
                        {
                            sManager.ReplaceCustomizationFilePart("ImportExportXml", (XmlNode)item.Tag);
                        }
                        else
                        {
                            sManager.ReplaceCustomizationFilePart("ImportExportXml/Entities/Entity[Name/@LocalizedName=\"" + item.Text + "\"]", (XmlNode)item.Tag);
                        }
                    }

                    this.ccsb.SetMessage("Preparing solution files...");
                    sManager.SaveRibbon();

                    this.ccsb.SetMessage("Importing solution...");
                    sManager.ImportSolution(this.ccsb, true);

                    this.ccsb.SetMessage("Import Done!");
                    MessageBox.Show(this, "Import done!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception error)
                {
                    MessageBox.Show(this, error.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                this.Cursor = Cursors.Default;
            };

            if (this.InvokeRequired)
            {
                this.Invoke(process);
            }
            else
            {
                process();
            }
        }

        private void tsbMainSaveFiles_Click(object sender, EventArgs e)
        {
            try
            {
                sManager.SaveRibbon();
            }
            catch (Exception error)
            {
                MessageBox.Show(this, error.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion

        #region Ribbon Treeview associated methods

        private void treeRibbon_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            object tag = e.Node.Tag;

            if ((tag is IEnableRule || tag is IDisplayRule || tag is ITabDisplayRule) && e.Node.Text != "OrRule")
            {
                UserControl ctrl = UserControlHelper.GetRuleUserControl(e.Node.Text, tag);
                this.DisplayInformationControl(ctrl);
            }
            else if (tag is EnableRule || tag is DisplayRule)
            {
                RuleControl ctrl = new RuleControl((IRule)tag);
                this.DisplayInformationControl(ctrl);
            }
            else if (tag is TabDisplayRule)
            {
                TabDisplayRuleControl ctrl = new TabDisplayRuleControl((TabDisplayRule)tag);
                this.DisplayInformationControl(ctrl);
            }
            else if (tag is ICommandUIDefinition)
            {
                UserControl ctrl = UserControlHelper.GetCommandUIDefinitionUserControl(e.Node.Text, tag);
                this.DisplayInformationControl(ctrl);
            }
            else if (tag is IAction)
            {
                UserControl ctrl = UserControlHelper.GetActionUserControl(e.Node.Text, tag);
                this.DisplayInformationControl(ctrl);
            }
            else if (tag is IParameter)
            {
                UserControl ctrl = UserControlHelper.GetParameterUserControl(e.Node.Text, tag);
                this.DisplayInformationControl(ctrl);
            }
            else if (tag is ITemplate)
            {
                UserControl ctrl = UserControlHelper.GetTemplateUserControl(e.Node.Text, tag);
                this.DisplayInformationControl(ctrl);
            }
            else if (tag is LocLabel)
            {
                LocLabelControl ctrl = new LocLabelControl((LocLabel)e.Node.Tag);
                this.DisplayInformationControl(ctrl);
            }
            else if (tag is Title)
            {
                TitleControl ctrl = new TitleControl((Title)e.Node.Tag);
                this.DisplayInformationControl(ctrl);
            }
            else if (tag is CommandDefinition)
            {
                CommandDefinitionControl ctrl = new CommandDefinitionControl((CommandDefinition)tag);
                this.DisplayInformationControl(ctrl);
            }
            else if (tag is ICustomAction)
            {
                if (tag is CustomAction)
                {
                    CustomActionControl ctrl = new CustomActionControl((CustomAction)tag);
                    this.DisplayInformationControl(ctrl);
                }
                else
                {
                    HideCustomActionControl ctrl = new HideCustomActionControl((HideCustomAction)tag);
                    this.DisplayInformationControl(ctrl);
                }
            }
            else
            {
                this.DisplayInformationControl(null);
            }

            this.ManageActionsDisplay();
        }

        private void tsbRibbonDelete_Click(object sender, EventArgs e)
        {
            UIActionHelper.Delete(this.treeRibbon.SelectedNode);

            this.ManageActionsDisplay();
        }

        private void tsbAddXmlContent_Click(object sender, EventArgs e)
        {
            AddXmlForm axForm = new AddXmlForm();
            axForm.StartPosition = FormStartPosition.CenterParent;

            if (axForm.ShowDialog() == DialogResult.OK)
            {
                XmlNode importedNode = sManager.CustomizationFile.ImportNode(axForm.AddedXmlNode, true);

                object tag = this.treeRibbon.SelectedNode.Tag;

                // TODO à implémenter
            }
        }

        #endregion

        #region Ribbons list associated methods

        private void lvItems_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            if (!e.IsSelected)
            {
                e.Item.Tag = currentRibbon;
            }
            else
            {
                if (this.lvItems.SelectedItems.Count > 0)
                {
                    currentRibbon = (XmlNode)this.lvItems.SelectedItems[0].Tag;

                    this.LoadRibbonTreeView(currentRibbon);
                }
            }

            this.DisplayInformationControl(null);
            this.ManageActionsDisplay();
        }

        private void tssbAdd_ButtonClick(object sender, EventArgs e)
        {
            addEntityRibbonToolStripMenuItem_Click(sender, e);
        }

        private void addApplicationRibbonToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.lvItems.Items.Count == 0)
            {
                if (sManager == null)
                {
                    sManager = new SolutionManager(service);
                }

                sManager.CustomizationFile = new XmlDocument();
                sManager.CustomizationFile.LoadXml(Variables.CUSTOMIZATION_BASE);
            }

            sManager.AddComponent(50, CreateSolutionDialog.RetrieveRibbonCustomization(service).Id, ":RibbonDiffXml", null);

            #region OLD treatment
            /*
            AddSolutionComponentRequest request = new AddSolutionComponentRequest()
            {
                ComponentType = 50, // Application Ribbon
                SolutionUniqueName = sManager.CurrentSolution["uniquename"].ToString(),
                ComponentId = CreateSolutionDialog.RetrieveRibbonCustomization(service).Id
            };

            service.Execute(request);
                     

            // Adds new node to customization file
            XmlTextReader xmlReader = new XmlTextReader(new StringReader(Variables.APPLICATION_RIBBON_BASE));
            XmlDocument xmlDocument = new XmlDocument();
            XmlNode node = xmlDocument.ReadNode(xmlReader);

            XmlNode importedNode = sManager.CustomizationFile.ImportNode(node, true);
            sManager.CustomizationFile.DocumentElement.AppendChild(importedNode);
            */
            #endregion

            // Adds item to items listview
            ListViewItem item = new ListViewItem("Application Ribbon");
            item.Tag = sManager.CustomizationFile.SelectSingleNode("ImportExportXml/RibbonDiffXml");
            item.Group = this.lvItems.Groups["lvgGeneral"];

            this.lvItems.Items.Add(item);
            this.ManageActionsDisplay();
        }

        private void addEntityRibbonToolStripMenuItem_Click(object sender, EventArgs e)
        {
            EntitiesPicker ePicker = new EntitiesPicker(this.mManager);
            ePicker.StartPosition = FormStartPosition.CenterParent;

            if (ePicker.ShowDialog() == DialogResult.OK)
            {
                if (this.lvItems.Items.Count == 0)
                {
                    if (sManager == null)
                    {
                        sManager = new SolutionManager(null);
                        sManager.CustomizationFile = new XmlDocument();
                        sManager.CustomizationFile.LoadXml(Variables.CUSTOMIZATION_BASE);
                    }
                }

                foreach (EntityMetadata emd in ePicker.SelectedEntities)
                {
                    // Vérification de la non existence de l'entité dans la liste des rubans
                    if (sManager.CustomizationFile.SelectSingleNode("ImportExportXml/Entities/Entity[Name/@LocalizedName=\"" + emd.DisplayName.UserLocalizedLabel.Label + "\"]") == null)
                    {
                        sManager.AddComponent(1, emd.MetadataId.Value, emd.LogicalName, emd);

                        #region old

                        /*
                        AddSolutionComponentRequest request = new AddSolutionComponentRequest()
                       {
                           ComponentType = 1, // Entity
                           SolutionUniqueName = sManager.CurrentSolution["uniquename"].ToString(),
                           ComponentId = mManager.EntityCache.Find(delegate(EntityMetadata innerEmd) { return innerEmd.LogicalName == emd.LogicalName; }).MetadataId.Value
                       };

                        service.Execute(request);

                        // Adds new node to customization file
                        string sNode = string.Format(Variables.ENTITY_RIBBON_BASE,
                            emd.DisplayName.UserLocalizedLabel.Label,
                            emd.SchemaName,
                            emd.SchemaName,
                            emd.ObjectTypeCode.Value);

                        XmlTextReader xmlReader = new XmlTextReader(new StringReader(sNode));
                        XmlDocument xmlDocument = new XmlDocument();
                        XmlNode node = xmlDocument.ReadNode(xmlReader);

                        XmlNode importedNode = sManager.CustomizationFile.ImportNode(node, true);
                        sManager.CustomizationFile.SelectSingleNode("ImportExportXml/Entities").AppendChild(importedNode);
                        */
                        #endregion

                        // Adds item to items listview
                        ListViewItem item = new ListViewItem(emd.DisplayName.UserLocalizedLabel.Label);
                        item.Tag = sManager.CustomizationFile.SelectSingleNode("ImportExportXml/Entities/Entity[Name/@LocalizedName=\"" + item.Text + "\"]/RibbonDiffXml");
                        item.Group = this.lvItems.Groups["lvgEntities"];

                        this.lvItems.Items.Add(item);
                    }
                }

                this.ManageActionsDisplay();
            }
        }

        private void tsbDelete_Click(object sender, EventArgs e)
        {
            if (this.lvItems.SelectedItems.Count > 0)
            {
                XmlNode node = (XmlNode)this.lvItems.SelectedItems[0].Tag;

                if (node.Name == "RibbonDiffXml")
                {
                    sManager.RemoveComponent(50, CreateSolutionDialog.RetrieveRibbonCustomization(service).Id, ":RibbonDiffXml");
                }
                else
                {
                    EntityMetadata emd = mManager.EntityCache.Find(delegate(EntityMetadata emdf) { return emdf.LogicalName == node.SelectSingleNode("Name").Attributes["SchemaName"].Value.ToLower(); });

                    sManager.RemoveComponent(1, emd.MetadataId.Value, emd.LogicalName);
                }

                this.treeRibbon.Nodes.Clear();
                this.lvItems.Items.Remove(this.lvItems.SelectedItems[0]);
                this.ManageActionsDisplay();
            }
        }

        private void addXmlContentToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddXmlForm axForm = new AddXmlForm();
            axForm.StartPosition = FormStartPosition.CenterParent;

            if (axForm.ShowDialog() == DialogResult.OK)
            {
                XmlNode importedNode = sManager.CustomizationFile.ImportNode(axForm.AddedXmlNode, true);

                switch (importedNode.Name)
                {
                    case "RibbonDiffXml":
                        if (sManager.CustomizationFile.SelectSingleNode("ImportExportXml/RibbonDiffXml") != null)
                        {
                            MessageBox.Show(this, "Only one application ribbon is allowed!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        else
                        {
                            sManager.CustomizationFile.DocumentElement.AppendChild(importedNode);

                            // Adds item to items listview
                            ListViewItem item = new ListViewItem("Application Ribbon");
                            item.Tag = sManager.CustomizationFile.SelectSingleNode("ImportExportXml/RibbonDiffXml");
                            item.Group = this.lvItems.Groups["lvgGeneral"];

                            this.lvItems.Items.Add(item);
                        }
                        break;
                    case "Entity":
                        {
                            XmlNode nameNode = importedNode.SelectSingleNode("Name");
                            if (nameNode == null || nameNode.Attributes["OriginalName"] == null)
                            {
                                MessageBox.Show(this, "Invalid 'Entity' Xml!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                            else
                            {
                                if (sManager.CustomizationFile.SelectSingleNode("ImportExportXml/Entities/Entity/Name[@OriginalName='" + nameNode.Attributes["OriginalName"].Value + "']") != null)
                                {
                                    MessageBox.Show(this, "Only one ribbon for entity '" + nameNode.Attributes["OriginalName"].Value + "' is allowed!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                }
                                else
                                {
                                    sManager.CustomizationFile.SelectSingleNode("ImportExportXml/Entities").AppendChild(importedNode);

                                    // Adds item to items listview
                                    ListViewItem item = new ListViewItem(nameNode.Attributes["LocalizedName"].Value);
                                    item.Tag = sManager.CustomizationFile.SelectSingleNode("ImportExportXml/Entities/Entity[Name/@OriginalName='" + nameNode.Attributes["OriginalName"].Value + "']/RibbonDiffXml");
                                    item.Group = this.lvItems.Groups["lvgEntities"];

                                    this.lvItems.Items.Add(item);
                                }
                            }
                        }
                        break;
                    default:
                        break;
                }

                this.ManageActionsDisplay();
            }
        }

        #endregion

        #region Properties Panel Methods

        private void tsbItemSave_Click(object sender, EventArgs e)
        {
            ((IControlRulable)this.currentDisplayedControl).Save();
            currentRibbon = this.ribbonDiffXml.GetRibbonDiffXmlDocument().DocumentElement;
        }

        private void tsbItemDisplayXml_Click(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.AppendChild(doc.CreateElement("Root"));

            object selectedItem = this.treeRibbon.SelectedNode.Tag;

            if (selectedItem is IXmlBuildable)
            {
                ((IXmlBuildable)selectedItem).AddNode(doc.DocumentElement);
            }

            if (doc.DocumentElement.ChildNodes.Count > 0)
            {
                XmlContentDisplayDialog dialog = new XmlContentDisplayDialog(doc.DocumentElement.InnerXml);
                dialog.StartPosition = FormStartPosition.CenterParent;
                dialog.ShowDialog();
            }
            else
            {
                MessageBox.Show(this, "Node type not supported", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        #endregion

        private void tsbAbout_Click(object sender, EventArgs e)
        {
            AboutForm form = new AboutForm();
            form.StartPosition = FormStartPosition.CenterParent;
            form.ShowDialog();
        }

        private void tsbRate_Click(object sender, EventArgs e)
        {
            Process.Start("http://ribboneditor.codeplex.com/releases");
        }

        private void tsbDiscuss_Click(object sender, EventArgs e)
        {
            Process.Start("http://ribboneditor.codeplex.com/discussions");
        }

        private void tsbReportBug_Click(object sender, EventArgs e)
        {
            Process.Start("http://ribboneditor.codeplex.com/WorkItem/Create");
        }

        private void tsbDonate_Click(object sender, EventArgs e)
        {
            string url = "";

            string business = "tanguy92@hotmail.com";  // your paypal email
            string description = "Donation%20for%20MSCRM%20Tools";            // '%20' represents a space. remember HTML!
            string country = "FR";                  // AU, US, etc.
            string currency = "EUR";                 // AUD, USD, etc.

            url += "https://www.paypal.com/cgi-bin/webscr" +
                "?cmd=" + "_donations" +
                "&business=" + business +
                "&lc=" + country +
                "&item_name=" + description +
                "&currency_code=" + currency +
                "&bn=" + "PP%2dDonationsBF";

            System.Diagnostics.Process.Start(url);

        }
    }
}
